package elevator;

import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;

public class writeToSocketThread extends Thread {

	protected LinkedBlockingQueue<String> queue;
	protected Socket socket;
	public PrintWriter out;
	
	public writeToSocketThread(){
		try {
			socket = new Socket("localhost", 4567);
			out = new PrintWriter(socket.getOutputStream(), true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		queue = new LinkedBlockingQueue<String>();
	}
	
	@Override
	public void run() {
		while(true){
			sendFirst();
		}
	}
	
	public void sendToElevators(String command){
		queue.offer(command);
	}
	
	protected void sendFirst(){
		try {
			out.println(queue.take());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
